package ${packagePath};

import com.zwb.demo.easyweb.common.entity.response.CommonCode;
import com.zwb.demo.easyweb.common.entity.response.ResponseResult;
import com.zwb.demo.easyweb.core.exception.ExceptionCast;
import com.zwb.demo.easyweb.core.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * ${tableDesc}
 *
 * @author ${author}
 * @date ${generateDate}
 */
@Service
public class ${className}ServiceImpl implements ${className}Service {
    private final ${className}PORepository ${className?uncap_first}Repository;

    @Autowired
    public ${className}ServiceImpl(${className}PORepository ${className?uncap_first}Repository) {
        this.${className?uncap_first}Repository = ${className?uncap_first}Repository;
    }
    

    @Override
    public ResponseResult saveOne(${className}VO vo) {
        if (StringUtils.isNotBlank(vo.getId())){
            Optional<${className}PO> optional = ${className?uncap_first}Repository.findById(vo.getId());
            if (!optional.isPresent()){
                ExceptionCast.cast(CommonCode.ID_NO_EXITS);
            }
            ${className}PO po = optional.get();
            BeanUtils.copyProperties(vo, po);
            ${className?uncap_first}Repository.save(po);
        }else {
            ${className}PO po = ConvertUtils.convertObject(vo, ${className}PO.class);
            ${className?uncap_first}Repository.save(po);
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult deleteOne(String uuid) {
        if (StringUtils.isBlank(uuid)){
            ExceptionCast.cast(CommonCode.ILLEGAL_PARAM);
        }
        ${className?uncap_first}Repository.deleteById(uuid);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult findAll() {
        List<${className}PO> allPO = ${className?uncap_first}Repository.findAll();
        List<${className}VO> allVO = ConvertUtils.convertList(allPO, ${className}VO.class);
        return ResponseResult.ok().body(allVO);
    }
}
